package 笔试.腾讯;

import 算法.NiuKe.ListNode;

/**
 * @author:谢君臣
 * @Date:2021/9/5-20:29
 * @version:1.0
 * @function:
 */
public class Main9500 {
    public static ListNode solve(ListNode[] a) {
        // write code here
        ListNode res = new ListNode(0);
        ListNode h, s;
        h = res;
        int n = a.length;
        int maxLen = getLength(a);
        while (maxLen-- > 0) {
            for (int i = 0; i < n; i++) {
                if (a[i] != null) {
                    s = new ListNode(a[i].val);
                    h.next = s;
                    h = h.next;
                    a[i] = a[i].next;
                }
            }
        }
        return res.next;
    }

    public static int getLength(ListNode[] a) {
        int n = a.length;
        int max = Integer.MIN_VALUE;
        ListNode node;
        int i = 0;
        while (i<n){
            int len = 0;
            node = a[i];
            while (node != null) {
                len++;
                node = node.next;
            }
            if (len > max)
                max = len;
            i++;
        }
        return max;
    }
}
